from django.shortcuts import render
from rest_framework.generics import ListAPIView, RetrieveAPIView
from .serializers import SpitListSerializer, SpitDetailSerializer, CommentSerializer,SpitCreateSerializer
from .models import Spit, Comment
from rest_framework.generics import CreateAPIView
from rest_framework.views import APIView
from rest_framework.response import Response


# Create your views here.


class SpitListView(ListAPIView):
    queryset = Spit.objects.all().order_by('create_time')
    serializer_class = SpitListSerializer


class SpitRetrieveAPIView(RetrieveAPIView):
    serializer_class = SpitDetailSerializer
    queryset = Spit.objects.all()


class CommentListAPIView(ListAPIView):
    queryset = Comment.objects.all().order_by('create_time')
    serializer_class = CommentSerializer


class SpitCreateAPIView(CreateAPIView):
    serializer_class = SpitCreateSerializer
    # def post(self, request):
    #     serializer = SpitCreateSerializer(data=request.data)
    #     serializer.is_valid(raise_exception=True)
    #     digested = serializer.validated_data['digested']
    #     content = serializer.validated_data['content']
    #     try:
    #         user = request.user
    #     except Exception:
    #         # 验证失败，用户未登录
    #         user = None
    #     if user:
    #         Spit.objects.create(
    #             digested=digested,
    #             content=content,
    #             user_id=user.id,
    #             source=user
    #         )
    #     return Response(serializer.data)
